package com.freesun.shop.controller;

import com.freesun.shop.model.WechatMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

@RestController
@RequestMapping("/p")
public class MessageController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private JmsTemplate jmsTemplate;



    private static final String MEM_BIND_PHONE_NUM_CODE_PREFIX = "mem:validation:code";

    @ApiOperation("获取验证码")
    @GetMapping("/wechat/send")
    public ResponseEntity<Void> sendValidationCode(String phonenum) {
        String code ="1234";
        redisTemplate.opsForValue().set(MEM_BIND_PHONE_NUM_CODE_PREFIX+phonenum,code, Duration.ofMinutes(1));
        WechatMessage wechatMessage = new WechatMessage(phonenum, "mTvR1j6jhOChK91X4HDuOAZkPE3d7ILldGYaJTd5o24", "www.egou.com");
        wechatMessage.append("user", "枫格", "#4169E1");
        wechatMessage.append("code", code, "#4169E1");
        wechatMessage.append("time", "1分钟", "#4169E1");
        jmsTemplate.convertAndSend("validation.code.queue",wechatMessage);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("获取短信验证码")
    @GetMapping("/sms/send")
    public ResponseEntity<Void> sendSmsMsg(String phonenum) {
        String code ="1234";
        redisTemplate.opsForValue().set(MEM_BIND_PHONE_NUM_CODE_PREFIX+phonenum,code, Duration.ofMinutes(1));
        String content=phonenum+"##"+code;
        jmsTemplate.convertAndSend("validation.code.sms.queue",content);
        return ResponseEntity.ok().build();
    }
}
